﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;

namespace Budget
{
    public partial class BarGraph : Form
    {
        public BarGraph()
        {
            InitializeComponent();
            foreach (string s in dbData.listExpenseCategories)
            {
                ddlCategory.Items.Add(s);
            }
            ddlCategory.SelectedIndex = 0;
            GetData();

            //zgc.
        }
        // Call this method from the Form_Load method, passing your ZedGraphControl
        public void CreateChart(ZedGraphControl zgc)
        {
            GraphPane myPane = zgc.GraphPane;

            // Set the title and axis labels
            myPane.Title.Text = "Vertical Bars with Value Labels Above Each Bar";
            myPane.XAxis.Title.Text = "Position Number";
            myPane.YAxis.Title.Text = "Some Random Thing";

            PointPairList list = new PointPairList();
            PointPairList list2 = new PointPairList();
            PointPairList list3 = new PointPairList();
            Random rand = new Random();

            // Generate random data for three curves
            for (int i = 0; i < 5; i++)
            {
                double x = (double)i;
                double y = rand.NextDouble() * 1000;
                double y2 = rand.NextDouble() * 1000;
                double y3 = rand.NextDouble() * 1000;
                list.Add(x, y);
                list2.Add(x, y2);
                list3.Add(x, y3);
            }

            // create the curves
            BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
            BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
            BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);

            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White,
               Color.FromArgb(255, 255, 166), 45.0F);

            zgc.AxisChange();

            // expand the range of the Y axis slightly to accommodate the labels
            myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

            // Create TextObj's to provide labels for each bar
            BarItem.CreateBarLabels(myPane, false, "f0");

        }
        public DataTable dt;
        private ZedGraph.ZedGraphControl zgc;
        public void GetData()
        {
            if(zgc != null)
            {
                zgc.Dispose();
            }
            
            zgc = new ZedGraph.ZedGraphControl();
            this.SuspendLayout();
            this.zgc.EditButtons = System.Windows.Forms.MouseButtons.Left;
            this.zgc.Location = new System.Drawing.Point(12, 43);
            this.zgc.Name = "zedGraphControl1";
            this.zgc.PanModifierKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Shift | System.Windows.Forms.Keys.None)));
            this.zgc.ScrollGrace = 0D;
            this.zgc.ScrollMaxX = 0D;
            this.zgc.ScrollMaxY = 0D;
            this.zgc.ScrollMaxY2 = 0D;
            this.zgc.ScrollMinX = 0D;
            this.zgc.ScrollMinY = 0D;
            this.zgc.ScrollMinY2 = 0D;
            this.zgc.Size = new System.Drawing.Size(808, 415);
            this.zgc.TabIndex = 0;
            this.Controls.Add(this.zgc);
            DateTime startDate = DateTime.Now;
            startDate = startDate.AddMonths(-4);
            startDate = Dates.getLastTransactionDayofMonth(startDate);
            startDate = startDate.AddDays(1);
            DateTime endDate = DateTime.Now;
            endDate = Dates.getFridayAssociatedWithDate(endDate);
            dt = DatabaseQuery.ExecStringProc(
                StoredProcs.getYearToDateByCategoryForMonth(startDate.ToString("MM/dd/yyyy"), endDate.ToString("MM/dd/yyyy"), ddlCategory.SelectedItem.ToString())
                );


            GraphPane myPane = zgc.GraphPane;

            // Set the title and axis labels
            myPane.Title.Text = "Vertical Bars with Value Labels Above Each Bar";
            myPane.XAxis.Title.Text = "Position Number";
            myPane.YAxis.Title.Text = "Some Random Thing";

            PointPairList list = new PointPairList();

            Random rand = new Random();

            int i = 0;
            foreach (DataRow dr in dt.Rows)
            {
                list.Add((double)i, Convert.ToDouble(dr["Amount"].ToString()));
                i++;
            }

            // create the curves
            BarItem myCurve = myPane.AddBar("curve 1", list, Color.Blue);
            //BarItem myCurve2 = myPane.AddBar("curve 2", list2, Color.Red);
            //BarItem myCurve3 = myPane.AddBar("curve 3", list3, Color.Green);

            // Fill the axis background with a color gradient
            myPane.Chart.Fill = new Fill(Color.White,
               Color.FromArgb(255, 255, 166), 45.0F);

            zgc.AxisChange();

            // expand the range of the Y axis slightly to accommodate the labels
            myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep;

            // Create TextObj's to provide labels for each bar
            BarItem.CreateBarLabels(myPane, false, "f0");
            
        }

        private void ddlCatetory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetData();
        }


    }
}
